package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.AddCookStepEntity;
import com.dllo.lanou.cookapp.entity.Collection;
import com.dllo.lanou.cookapp.entity.User;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xijinhua on 2016/5/7.
 */
public class MyListCookAdapter  extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private List<Collection> list;

    public MyListCookAdapter(Context context,List<Collection> list){
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    public MyListCookAdapter(Context context){
        this.context = context;
        mInflater = LayoutInflater.from(context);


    }
    public void  addData(List<Collection> list){;
        this.list = list;
        Log.i("addCookStepEntities", list.size() + "");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list!= null && list.size()>0 ?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position)!=null && list.size()>0?list.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null){

            convertView =mInflater.inflate(R.layout.item_showcooking,null);
            Log.i("收藏的布局",convertView+"");
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        //加载数据
        Collection collection =   list.get(position);
        AddCookStepEntity addCookStepEntity = collection.getAddCookStepEntity();
        //Log.i("addCookStepEntities",addCookStepEntity.getName()+"");

        if (addCookStepEntity.getName() !=null){
            holder.show_cooking_cookname.setText(addCookStepEntity.getName());
        }
        if (addCookStepEntity.getBmob_img() != null){
            GetVolleyImage.getNetWorkImg(addCookStepEntity.getBmob_img(), holder.show_cooking_img);
        }
        User user = addCookStepEntity.getAuthor();
        holder.username.setText(user.getUsername());
        if (user.getImg() != null) {
            Log.i("图片collections",user.getImg()+"");
            user.getImg().loadImage(context, holder.show_cooking_head);
        }
//        }

        return convertView;
    }

    static class ViewHolder{
        @Bind(R.id.show_cooking_head)
        ImageView show_cooking_head;
        @Bind(R.id.show_cooking_img)
        ImageView show_cooking_img;
        @Bind(R.id.show_cooking_username)
        TextView username;
        @Bind(R.id.show_cooking_cookname)
        TextView  show_cooking_cookname;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }



}